import socketserver,random

class MyServer(socketserver.BaseRequestHandler):
    def setup(self):
        pass
    def handle(self):
        connection=self.request
        msg='Hello World'
        connection.send(msg.encode(encoding='utf-8'))
        while True:
            data=connection.recv(1024)
            print(data.decode(encoding='utf-8'))
            if data == b'exit':
                break
            connection.send(data)
            connection.send(str(random.randint(1,1000)).encode(encoding='utf-8'))
        connection.close()
    def finish(self):
        pass
if __name__ == '__main__':
    server=socketserver.ThreadingTCPServer(('127.0.0.1',8888),MyServer)
    server.serve_forever()
